package com.intretech.umsin.oss.service.impl;

import com.intretech.umsin.oss.config.SftpOSSConfig;
import com.intretech.umsin.oss.entity.FileType;
import com.intretech.umsin.oss.service.MetadataStoreService;
import com.intretech.umsin.oss.util.SftpOssClient;
import com.intretech.umsin.oss.vo.FileInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @version V1.0
 * @ClassName: SftpOssServiceImpl
 * @Description: SftpOssServiceImpl SFTP实现的OSS
 * @author: 李启联
 * @date: 2022-04-25 22:57
 * @Copyright: Copyright (c) 2022
 */
@Service
@Slf4j
public class SftpOssServiceImpl extends AbstractFileService{

    @Autowired
    private MetadataStoreService metadataStoreService;

    @Autowired(required = false)
    private SftpOSSConfig sftpConfig;

    @Autowired(required = false)
    private SftpOssClient sftpOssClient;
    /**
     * OSS存储类型
     *
     * @return
     */
    @Override
    public FileType fileType() {
        return FileType.SFTP;
    }

    /**
     * 文件元数据存储服务
     *
     * @return
     */
    @Override
    protected MetadataStoreService getMetadataStoreService() {
        return metadataStoreService;
    }

    /**
     * 上传文件
     *
     * @param file
     * @param fileInfo
     */
    @Override
    protected void uploadFile(MultipartFile file, FileInfoVo fileInfo) throws Exception {
        String workDir = sftpConfig.getWorkDir().replace("\\","/");
        workDir = workDir.startsWith("/") ? workDir : "/"+workDir;
        workDir = workDir.endsWith("/") ?  workDir.substring(0,workDir.length() -1) : workDir;

        if(fileInfo.getFilePathPrefix().length() >1){//设置文件的路径前缀
            String prefix = fileInfo.getFilePathPrefix().replace("\\","/");
            prefix = prefix.startsWith("/") ? prefix : "/"+prefix;
            prefix = prefix.endsWith("/") ? prefix.substring(0,prefix.length() -1) : prefix;
            fileInfo.setFilePathPrefix(workDir + prefix);
        }else{
            fileInfo.setFilePathPrefix(workDir);
        }
        try {
            sftpOssClient.upload(file.getInputStream(),fileInfo.getFilePathPrefix() +"/"+ fileInfo.getTagsName());
        }
        finally {
            sftpOssClient.closeChannel();
        }
    }

    /**
     * 删除文件资源
     *
     * @param fileInfo
     * @return
     */
    @Override
    protected boolean deleteFile(FileInfoVo fileInfo) {
        try {
            sftpOssClient.deleteFile(fileInfo.getPath());
            return true;
        }catch (Exception e) {
            return false;
        }finally {
            try {
                sftpOssClient.closeChannel();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 对查询返回的文件列表设置其对应文件访问的URL
     *
     * @param fileInfo
     */
    @Override
    protected void setFileUrl(FileInfoVo fileInfo) {
        fileInfo.setUrl(sftpConfig.getDomain() + fileInfo.getPath());
    }

    /**
     * 下载文件
     *
     * @param fileInfo
     * @return
     */
    @Override
    public byte[] downloadFile(FileInfoVo fileInfo) throws Exception {
        try {
            return sftpOssClient.download(fileInfo.getPath());
        } finally {
            sftpOssClient.closeChannel();
        }
    }

}
